﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TCPClient.RequestsToServerHandlers;
using ApplLogic.ApplClasses;
using TCPClient.UI.UserControls;
using ConnectionObjects;

namespace TCPClient.UI.LogicForUserControls
{
    public class ScMainUserControlLogic : ScMainUserControlLogicBase
    {
        private static ScMainUserControlLogic m_Instance = null;

        public static void Create(RequestsToServerHandler handler)
        {
            if(m_Instance == null)
            {
                m_Instance = new ScMainUserControlLogic(handler);
            }
        }

        public static ScMainUserControlLogic Instance
        {
            get { return m_Instance; }
        }

        private ScMainUserControlLogic(RequestsToServerHandler handler)
            :base(handler)
        {
            
        }

        private RequestsToServerHandlerForWorker downCast()
        {
            return m_HandlerBase as RequestsToServerHandlerForWorker;
        }

        public ApplWorker ShowWorkerDetails()
        {
            OutConnectionObject worker = downCast().ShowWorkerDetails(downCast().LoggedInWorker.PersonalDetails.Id);
            return worker.RetVal as ApplWorker;
        }

        public List<ApplShift> GetAllWantedShifts()
        {
            OutConnectionObject result = downCast().GetAllShiftsThatWorkerAskedWithType(
                downCast().LoggedInWorker.PersonalDetails.Id, eType.WANTED, m_StartTimeOfTheWeek.AddHours(-1), m_EndTimeOfTheWeek.AddHours(1));
            return result.RetVal as List<ApplShift>;
        }

        public List<ApplShift> GetAllForbiddenShifts()
        {
            OutConnectionObject result = downCast().GetAllShiftsThatWorkerAskedWithType(
                downCast().LoggedInWorker.PersonalDetails.Id, eType.FORBIDDEN, m_StartTimeOfTheWeek.AddHours(-1), m_EndTimeOfTheWeek.AddHours(1));
            return result.RetVal as List<ApplShift>;
        }

        public List<ApplShift> GetAllRegisteredShifts()
        {
            OutConnectionObject result = downCast().GetAllShiftsThatWorkerIsRegisteredTo(
                downCast().LoggedInWorker.PersonalDetails.Id, m_StartTimeOfTheWeek.AddHours(-1), m_EndTimeOfTheWeek.AddHours(1));
            return result.RetVal as List<ApplShift>;
        }

        public void UpdateServerWithWorkerRequest(List<ShiftRequestFromGUI> requests)
        {
            List<ApplSingleRequest> workerRequest = convertTheUiRequestToApplRequest(requests);
            sendAllNewRequestsToServer(workerRequest);
        }

        private void sendAllNewRequestsToServer(List<ApplSingleRequest> workerRequest)
        {
            downCast().RenterAllRequestsOfWorker(downCast().LoggedInWorker.PersonalDetails.Id, m_StartTimeOfTheWeek,
                                                 m_EndTimeOfTheWeek,
                                                 workerRequest);
        }

        private List<ApplSingleRequest> convertTheUiRequestToApplRequest(List<ShiftRequestFromGUI> requests)
        {
            List<ApplSingleRequest> workerRequest = new List<ApplSingleRequest>();
            foreach (var shiftRequestFromGui in requests)
            {
                ApplSingleRequest request = new ApplSingleRequest();
                request.ApplWorker = downCast().LoggedInWorker;
                request.ApplShift = downCast().ShowShiftDetails(shiftRequestFromGui.StartDate,
                                                                            shiftRequestFromGui.EndDate).RetVal as ApplShift;
                request.Type = shiftRequestFromGui.Type;
                workerRequest.Add(request);
            }
            return workerRequest;
        }


        public void CopyRequestFrom()
        {
            downCast().CopyRequestFrom(downCast().LoggedInWorker.PersonalDetails.Id, m_StartTimeOfCurrentWeek, m_EndTimeOfCurrentWeek);
        }

        public void UpdateWorker()
        {
            downCast().UpdateWorker(downCast().LoggedInWorker);
        }
    }
}
